package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.dto.FilesAttentionDto;
import cn.cdeden.knowledge.domain.dto.FilesAttentionQueryParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cdeden.knowledge.domain.FilesAttention;
import cn.cdeden.knowledge.mapper.FilesAttentionMapper;
import cn.cdeden.knowledge.service.FilesAttentionService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FilesAttentionServiceImpl extends ServiceImpl<FilesAttentionMapper, FilesAttention> implements FilesAttentionService{

    private final FilesAttentionMapper fileAttentionMapper;

    @Override
    public List<String> getAttentionPeople(String treeNodeId, String fileType) {
        //TODO  错误：触发两次消息通知
        List<String> list = new ArrayList<>();
        FilesAttentionQueryParam queryParam = new FilesAttentionQueryParam();
        QueryWrapper<FilesAttention> predicate = QueryHelpMybatisPlus.getPredicate(queryParam);
        predicate.eq("tree_node_id",treeNodeId);
        predicate.eq("file_type",fileType);
        List<FilesAttentionDto> filesAttentionDtos = MapstructUtils.convert(fileAttentionMapper.selectList(predicate), FilesAttentionDto.class);
        for (int i = 0; i < filesAttentionDtos.size(); i++) {
            list.add(String.valueOf(filesAttentionDtos.get(i).getUserId()));
        }
        return list;
    }

    @Override
    public int insert(FilesAttentionDto resources) {
        FilesAttention entity = MapstructUtils.convert(resources, FilesAttention.class);
//        FilesAttention entity = ConvertUtil.convert(resources, FilesAttention.class);
        entity.setUserId(LoginHelper.getUserId());
//        entity.setUserId(String.valueOf(FileUserUtils.getUser().getId()));
        return fileAttentionMapper.insert(entity);
    }

    @Override
    public int removeByMap(FilesAttentionDto resources) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("file_type",resources.getFileType());
        map.put("user_id", LoginHelper.getUserId());
//        map.put("user_id",FileUserUtils.getUser().getId());
        map.put("tree_node_id",resources.getTreeNodeId());
        return fileAttentionMapper.deleteByMap(map);
    }

    @Override
    public FilesAttentionDto getAttention(String treeNodeId, String userId, String fileType) {
        FilesAttentionQueryParam queryParam = new FilesAttentionQueryParam();
        QueryWrapper<FilesAttention> predicate = QueryHelpMybatisPlus.getPredicate(queryParam);
        predicate.eq("tree_node_id",treeNodeId);
        predicate.eq("file_type",fileType);
        predicate.eq("user_id",userId);
        return MapstructUtils.convert(fileAttentionMapper.selectOne(predicate), FilesAttentionDto.class);
    }
}
